package io.demo.hallelujahspring5;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Optional;

import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;

/**
 * @author lijinting01
 */
@Slf4j
@Component
public class SomeJob extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext context) {
		ZoneId systemDefault = ZoneId.systemDefault();
		Date next = Optional.ofNullable(context.getNextFireTime()).orElse(new Date());
		Date prev = Optional.ofNullable(context.getPreviousFireTime()).orElse(new Date());
		log.info("FireInstanceId :{} PreviousFireTime:{} NextFireTime: {}",
				context.getFireInstanceId(),
				ISO_DATE_TIME.format(LocalDateTime.ofInstant(next.toInstant(), systemDefault)),
				ISO_DATE_TIME.format(LocalDateTime.ofInstant(prev.toInstant(), systemDefault)))
		;
	}
}
